package org.pettyfox.pfpay.notify;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.pettyfox.pfpay.pojo.PayAuditPojo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认通知的实现，使用钉钉
 */
@Component
@Slf4j
public class DefaultPFNotify implements PFNotify {
    @Override
    public void asyncNotifyPayAudit(PayAuditPojo payAuditPojo) {
        DingTalkClient client = new DefaultDingTalkClient(payAuditPojo.getToHook());
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype(DingDingNotify.MsgType.ACTION_CARD.value());

        List<OapiRobotSendRequest.Btns> btnsList = new ArrayList<>();
        OapiRobotSendRequest.Btns btn = new OapiRobotSendRequest.Btns();

        OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();


        actioncard.setTitle("【新付款待审核】");
        actioncard.setText(String.format("# 您有新的待审核订单  \n " +
                "- 订单id: %s  \n" +
                "- 付款金额: ¥ %s \n" +
                "- 下单时间: %s",payAuditPojo.getOrderId(),payAuditPojo.getRealAmount(),payAuditPojo.getOrderCreateTime()));


        actioncard.setSingleTitle("立即审核");
        actioncard.setSingleURL(payAuditPojo.getAuditUrl());
        request.setActionCard(actioncard);
        try {
            OapiRobotSendResponse response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
            log.error(e.getErrMsg());
        }
    }

}
